package org.xue.snowmall.bargain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import entity.GoodsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.xue.snowmall.bargain.feign.ProductFeignService;
import org.xue.snowmall.common.utils.PageUtils;
import org.xue.snowmall.common.utils.Query;

import org.xue.snowmall.bargain.dao.BargainDao;
import org.xue.snowmall.bargain.entity.BargainEntity;
import org.xue.snowmall.bargain.service.BargainService;
import org.xue.snowmall.common.utils.R;


@Service("bargainService")
public class BargainServiceImpl extends ServiceImpl<BargainDao, BargainEntity> implements BargainService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BargainEntity> page = this.page(
                new Query<BargainEntity>().getPage(params),
                new QueryWrapper<BargainEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R saveBargain(BargainEntity bargain) {
        bargain.setGsmBargainStatus("0");

        //TODO feign 调用查询商品信息
        R info = productFeignService.info(bargain.getPsmId());
        GoodsEntity goods = info.getData("goods", new TypeReference<GoodsEntity>() {
        });

        bargain.setPsmName(goods.getPsmName());
        bargain.setPsmImg(goods.getPsmImg());
        bargain.setPsmPrice(goods.getPsmPrice());
        baseMapper.insert(bargain);
        return R.ok();
    }



}